<?php

namespace Inter\ArticleBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;


class ArticleController extends Controller
{
	/**
	 * Show all articles
	 * 
	 */
    public function indexAction(Request $request)
    {    	
    	$em = $this->getDoctrine()->getEntityManager();   	    	
    	
    	$cache_key = 'article_cache_letstartworkinghard1';
    	$cache = $this->get('liip_doctrine_cache.ns.letstartworkinghard');    	
    	
    	if (false === ($cached_data = $cache->fetch($cache_key))) {
    		$cached_data = $em->getRepository('InterArticleBundle:Article')->getAllArticles();
    		$cache->save($cache_key, $cached_data, 3600);//TTL 1h
    	}
		
    	$paginator  = $this->get('knp_paginator');
    	$pagination = $paginator->paginate(
    			$cached_data,
    			$request->query->get('page', 1)/*page number*/,
    			2/*limit per page*/
    	);
    	if($request->isXmlHttpRequest()) {
    		return $this->render('InterArticleBundle:Event:index.html.twig', array(
        		'pagination' => $pagination
        	));
    	} else {    
    		return $this->render('InterArticleBundle:Default:index.html.twig', array(
    				'pagination' => $pagination
    		));
    	}
        
    } 
  
    
    /**
     * Show single Article
     */
	public function showAction($slug) 
    {
    	$em = $this->getDoctrine()->getEntityManager();
    	$article = $em->getRepository('InterArticleBundle:Article')->findOneBy(array('slug' => $slug));
    	
    	if (!$article) {
    		throw $this->createNotFoundException('Unable to find Article post.');
    	}
    	
    	return $this->render('InterArticleBundle:Default:show.html.twig', array(
    			'article'      => $article,
    	));
        
    	/**
    	$em = $this->getDoctrine()->getEntityManager();
    	$article = $em->getRepository('InterArticleBundle:Article')->findOneBySlug($slug);
    	 
    	if (!$article) {
    		throw $this->createNotFoundException('Unable to find Article post.');
    	}
    	 
    	return $this->render('InterArticleBundle:Default:show.html.twig', array(
    			'article'      => $article,
    	));
    	*/
    }
}
